<?php

namespace app\controllers;


use Yii;
use yii\data\ArrayDataProvider;
use zonday\weixin\api;

class UserController extends BaseController
{
    public function actionIndex($nextOpenid = '')
    {
        $userApi = new api\User();
        $result = $userApi->get($nextOpenid);
        $total = $result['total'];
        $count = $result['count'];
        $data = $result['data'];
        $nextOpenid = $result['next_openid'];

        $userList = [];
        foreach ($data['openid'] as $openid) {
            $userList[] = ['openid' => $openid];
        }

        $dataProvider = new ArrayDataProvider([
            'allModels' => $userApi->infoBatchGet($userList)['user_info_list']
        ]);

        return $this->render('index', [
            'openids' => $data['openid'],
            'total' => $total,
            'count' => $count,
            'dataProvider' => $dataProvider,
            'nextOpenid' => $nextOpenid,
        ]);
    }

    public function actionView($openid)
    {
        $userApi = new api\User();
        $user = $userApi->info($openid);
        return $this->render('view', [
            'user' => $user,
        ]);
    }

    public function actionUpdateRemark($openid)
    {
        $userApi = new api\User();
        $user = $userApi->info($openid);
        $remark = $user['remark'];

        $request = Yii::$app->getRequest();
        if ($request->isPost && $remark = $request->post('remark')) {
            $userApi->updateRemark($openid, $remark);
            Yii::$app->getSession()->setFlash('success', '更新用户备注成功');
            return $this->redirect(['index']);
        }

        return $this->render('updateRemark', [
            'remark' => $remark,
        ]);
    }

    public function actionChangeGroup($openid)
    {
        $groups = new api\Groups();
        $request = Yii::$app->getRequest();
        $openid = $request->post('openid', $openid);
        $groupId = $request->post('groupId', $groups->getId($openid)['groupid']);
        if ($request->isPost && $openid && $groupId !== null) {
            $groups->membersUpdate($openid, $groupId);
            Yii::$app->getSession()->setFlash('success', '更新用户组成功');
            return $this->redirect(['index']);
        }

        $groups =  $groups->get()['groups'];
        $groupsList = [];
        foreach ($groups as $group) {
            $groupsList[$group['id']] = $group['name'];
        }

        return $this->render('changeGroup', [
            'openid' => $openid,
            'groupId' => $groupId,
            'groupsList' => $groupsList,
        ]);
    }

    public function actionBatchChangeGroup()
    {
        $groups = new api\Groups();
        $request = Yii::$app->getRequest();
        $openids = $request->post('openids');
        $groupId = $request->post('groupId');
        if ($request->isPost && $openids && $groupId !== null) {
            $groups->membersBatchUpdate((array) $openids, $groupId);
            Yii::$app->getSession()->setFlash('success', '批量更新用户组成功');
            return $this->redirect(['index']);
        }

        $groups =  $groups->get()['groups'];
        $groupsList = [];
        foreach ($groups as $group) {
            $groupsList[$group['id']] = $group['name'];
        }

        $allOpenids = (new api\User())->get()['data']['openid'];
        $openidsList = array_combine($allOpenids, $allOpenids);

        return $this->render('BatchChangeGroup', [
            'openids' => $openids,
            'groupId' => $groupId,
            'groupsList' => $groupsList,
            'openidsList' => $openidsList
        ]);
    }
}